package com.activities;

import android.support.v4.app.Fragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fragments.GroupContactFragment;
import com.fragments.MyContactFragment;
import com.senditcertified.mail.R;

public class ContactAcitivity extends BaseFragmentActivity implements
		OnClickListener {
	private LinearLayout mLnContact;
	private LinearLayout mLnGroupContact;
	private int tabTextColorNormal;
	private int tabTextColorFocus;
	// value fields
	private boolean isContactNameAsc = true, isContactEmailAsc = true;// for
	private int contactSortType = MyContactFragment.SORT_NAME; // sort
	// String
	// in db
	private boolean isGroupNameAsc = true;// for sort String in db

	@Override
	protected int initLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_contact;
	}

	@Override
	protected void initViews() {
		mLnContact = (LinearLayout) findViewById(R.id.ln_contact);
		mLnGroupContact = (LinearLayout) findViewById(R.id.ln_group_contact);

	}

	@Override
	protected void initComponents() {
		tabTextColorNormal = getResources()
				.getColor(R.color.contact_tab_normal);
		tabTextColorFocus = getResources().getColor(R.color.contact_tab_focus);
	}

	@Override
	protected void onResume() {
		Fragment currentFragment = getSupportFragmentManager()
				.findFragmentById(R.id.frame_main);
		if (currentFragment instanceof GroupContactFragment) {
			GroupContactFragment fragment = (GroupContactFragment) currentFragment;
			fragment.getData();

		} else if (currentFragment instanceof MyContactFragment) {
			MyContactFragment fragment = (MyContactFragment) currentFragment;
			fragment.getData();

		} else {
			showContactFragment();
		}// for reload Data
		super.onResume();
	}

	@Override
	protected void addListener() {
		mLnContact.setOnClickListener(this);
		mLnGroupContact.setOnClickListener(this);
	}

	public void showContactFragment() {
		Fragment currentFragment = getSupportFragmentManager()
				.findFragmentById(R.id.frame_main);
		if (!(currentFragment instanceof MyContactFragment)) {
			MyContactFragment fragment = new MyContactFragment();
			if (fragment != null) {
				setNewPage(fragment);
			}
		}
	}

	public void showGroupFragment() {
		Fragment currentFragment = getSupportFragmentManager()
				.findFragmentById(R.id.frame_main);
		if (!(currentFragment instanceof GroupContactFragment)) {
			GroupContactFragment fragment = new GroupContactFragment();
			if (fragment != null) {
				setNewPage(fragment);
			}
		}
	}

	@Override
	public void onClick(View v) {
		if (v == mLnContact) {
			mLnContact.setBackgroundResource(R.drawable.btn_contact_tab_focus);
			((ImageView) mLnContact.getChildAt(0))
					.setImageResource(R.drawable.ic_contact_focus);
			((TextView) mLnContact.getChildAt(1))
					.setTextColor(tabTextColorFocus);

			mLnGroupContact.setBackgroundResource(R.drawable.button_tab);
			((ImageView) mLnGroupContact.getChildAt(0))
					.setImageResource(R.drawable.ic_group_contact_normal);
			((TextView) mLnGroupContact.getChildAt(1))
					.setTextColor(tabTextColorNormal);
			showContactFragment();
		} else if (v == mLnGroupContact) {
			mLnContact.setBackgroundResource(R.drawable.button_tab);
			((ImageView) mLnContact.getChildAt(0))
					.setImageResource(R.drawable.ic_contact_normal);
			((TextView) mLnContact.getChildAt(1))
					.setTextColor(tabTextColorNormal);

			mLnGroupContact
					.setBackgroundResource(R.drawable.btn_contact_tab_focus);
			((ImageView) mLnGroupContact.getChildAt(0))
					.setImageResource(R.drawable.ic_group_contact_focus);
			((TextView) mLnGroupContact.getChildAt(1))
					.setTextColor(tabTextColorFocus);
			showGroupFragment();
		}

	}

	public boolean isContactNameAsc() {
		return isContactNameAsc;
	}

	public void setContactNameAsc(boolean isContactNameAsc) {
		this.isContactNameAsc = isContactNameAsc;
	}

	public boolean isContactEmailAsc() {
		return isContactEmailAsc;
	}

	public void setContactEmailAsc(boolean isContactEmailAsc) {
		this.isContactEmailAsc = isContactEmailAsc;
	}

	public boolean isGroupNameAsc() {
		return isGroupNameAsc;
	}

	public void setGroupNameAsc(boolean isGroupNameAsc) {
		this.isGroupNameAsc = isGroupNameAsc;
	}

	public int getContactSortType() {
		return contactSortType;
	}

	public void setContactSortType(int contactSortType) {
		this.contactSortType = contactSortType;
	}

}
